#ifndef NETDEALWITH_H
#define NETDEALWITH_H

#include <QObject>
#include <QTimer>
#include <QMutex>
#include <QHash>
#include <QTime>
#include"netsendreceive.h"
#include"analytedatabase.h"
#include"experiment.h"

enum QueueExpStep
{
    EXP_GET_EXP=0,
    EXP_WAIT_BEFOR,
    EXP_GET_SAMPLE,
    EXP_WAIT_SAMPLE_IN,
    EXP_WAIT_STABLE1,
    EXP_SCAN_PRE,
    EXP_SCAN_MAIN,
    EXP_WAIT_WASH_IN,
    EXP_WAIT_STABLE2,
    EXP_WAIT_AFTER,
    EXP_WAIT_PRE,
    EXP_WAIT_MAIN,
    EXP_TIMEOUT,
    EXP_FINISH,
    EXP_HOLD
};

class NetDealwith : public QObject
{
    Q_OBJECT
public:
    explicit NetDealwith(NetSendReceive *pNetSendReceive=NULL,QObject *parent = 0);

    bool StartExp();
    void ProcessExp(int &expIndex,int &sampleIndex,int &preIndex,int &mainIndex);
    void SetWaitIn(int);//

    bool ProcessWorkFlow(QString name);//name:流程名
    bool ProcessStep(int &index);
    bool OpenVacuum();
    bool CloseVacuum();
    bool OpenPlasma();
    bool ClosePlasma();
    bool CheckAllCondition(QList<WorkStepCondition> );
    bool CheckCondition(double value,int index,double setValue);

    bool StartTune(bool state);
    //cmd-new
    bool ReadSerialNum();
    bool SetSerialNum(QString num);
    bool ReadSoftwareVersion(uchar type);
    bool ReadEnvironmentParas();
    bool SetEnvironmentParas1(WorkStep);//开关量
    bool SetEnvironmentParas2(WorkStep);//数值量
    bool ReadLenBoardParas();
    bool SetLenBoardParas(WorkStep);//
    bool ReadAlarm();
    bool SetAlarm(uchar length,QByteArray data);//
    bool ReadDetectorParas();
    bool SetDetectorParas(WorkStep);//
    bool SetLimits(LimitValue);//
    bool SetPoleCalRatio(CalRatio four,CalRatio six);
    bool SetSecurityAmu(float amu);
    bool SetBuildTimeCalRatio(double k,double b);
    bool StartScan(uchar type);
    bool SetHopScanParas(QList<ScanPara>,double,double,double,double,short);//待参数确定
    bool SetSweepScanParas(QList<ScanPara>,double,double,double,double,short);//待参数确定
    bool ReadScanResult();

    bool SetParas();
    bool ReadParas();
    //cmd-old
    /*
    bool ReadSerialNum();
    bool SetSerialNum(QString num);
    bool ReadSoftwareVersion(uchar type);
    bool ReadDetectorV(uchar type);//
    bool SetDetectorV(uchar type);//
    bool ReadLimits(uchar type);
    bool SetLimits(uchar type);
    bool ReadMotorParas();
    bool ReadSystemParas();
    bool ReadResolution(uchar type);
    bool SetResolution(uchar type);
    bool ReadCal(uchar type);
    bool SetCal(uchar type);
    bool SetAlarm(uchar length);//clear
    bool ReadGroundFire(uchar ground,uchar fire);
    bool SetGroundFire(uchar ground,uchar fire);
    bool ReadScreeningVol(uchar type);
    bool SetScreeningVol(uchar type);
    bool SetCollection(uchar type,uchar other);
    bool ReadHopPara();
    bool SetHopPara();
    bool ReadData();//
    bool SetMethod();//
    bool SetFrequency();
    bool SetHeterogeneous(uchar type);
    */
private:
    NetSendReceive *pNetSendReceive;
    QTimer *pTimerWork;
    QTimer *pTimerRead;
    QTimer *pTimerMonitor;
    int expType;//0:流程 1：实验 2：调谐扫描
    QString message;//

    int workState;
    int workSubState;
    int currentIndex;
    QTime holdTime;
    QList<WorkStep> workSteps;//执行等工作流程表
    QString workName;//执行等工作名
    //CurrentValue *pCurrentValue;
    WorkStep workStepSet;//
    WorkStep workStepRead;//

    int expSubStep;
    Experiment experiment;
    Experiment *pExperiment;
    ExpSample *pExpSample;
    SampleInfor sampleInfor;
    SampleInfor *pSampleInfor;
    PreScanResult *pPreScanResults;
    MainScanResult *pMainScanResults;
    QString filePath;
    int sampleIndex;
    int expIndex;
    int preIndex;
    int mainIndex;
    int waitIn;//0:no 1:yes 2:allyes
    QString expName;

    int sendState;//0:正常 1：异常
    SystemState systemState;

    //result
    QList<ExpRawData> expRawDatas;

    //
    QMutex mutex;
    QHash<QString,NetDataPacket> hashMap[20];

    void ResendAdd(NetDataPacket);
    void ResendDelete(NetDataPacket );
    void ResendDeleteAll();
    void StartStop(bool state,int type=0);
    void SetMessage(QString);

    void EncodeFloat(float,QByteArray &data,int startIndex);
    float DecodeFloat(QByteArray);
    void EncodeInt(int,QByteArray &data,int startIndex);
    int DecodeInt(QByteArray);
    void EncodeShort(short,QByteArray &data,int startIndex);
    int DecodeShort(QByteArray);
signals:
    void SIG_StateChanged(QString);
    void SIG_WorkFlowFinished(int);//

    void SIG_ExpStateChanged(QString);
    void SIG_WaitIn(int);
    void SIG_ExpScanChanged(QString path,int sampleIndex,int preIndex,int mainIndex);
    void SIG_ExpFinish(int);

    void SIG_ResponseTimeout(uchar,uchar);
    void SIG_ReadValue(WorkStep);
    void SIG_ReadResult(QList<ExpRawData>);
public slots:
    void OnStop();
private slots:
    void OnWorkTimeout();
    void OnReadTimeout();
    void Decode(NetDataPacket);
    void Encode(NetDataPacket);
    void OnMonitorTimerOut();//命令超时监控
};

#endif // NETDEALWITH_H
